package com.guan.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guan.eduservice.entity.EduChapter;
import com.guan.eduservice.entity.EduVideo;
import com.guan.eduservice.entity.chapter.ChapterVo;
import com.guan.eduservice.entity.chapter.VideoVo;
import com.guan.eduservice.mapper.EduChapterMapper;
import com.guan.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guan.eduservice.service.EduVideoService;
import com.guan.servicebase.exceptonhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程章节表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-01-01
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;


    /**
     * 根据课程id查询课程大纲列表
     */
    @Override
    public List<ChapterVo> getChapterVideoBycourseId(String courseId) {
        //根据课程id查询所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //根据课程id查询小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

        //创建list集合，封装数据
        List<ChapterVo> finalList = new ArrayList<>();

        //遍历查询章节的list集合进行封装
        for (int i = 0; i < eduChapterList.size(); i++) {
            EduChapter eduChapter = eduChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalList.add(chapterVo);

            //遍历查询小节的list集合进行封装
            List<VideoVo> videoVoList = new ArrayList<>();
            for (int m = 0; m < eduVideoList.size(); m++) {
                EduVideo eduVideo = eduVideoList.get(m);
                //小节的chapter-id = 章节的id?
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            //把封装好的小节list，放到对应的章节vo里
            chapterVo.setChildren(videoVoList);
        }

        return finalList;
    }

    /**
     * 删除章节
     * @param chapterId
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        //先查询章节有无小节
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        //List<EduVideo> list = videoService.list(wrapper);
        int count = videoService.count(wrapper);
        if (count>0) {
            throw new GuliException(20001,"该章节下有小节，不能删除");
        }else {
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }

    /**
     * 删除章节
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);

    }
}
